package com.building.service.impl;

import com.building.dao.DormitoryManagerDao;
import com.building.dao.StudentDao;
import com.building.entity.DormitoryManager;
import com.building.entity.Repairrecord;
import com.building.dao.RepairrecordDao;
import com.building.entity.Student;
import com.building.response.ServerResponse;
import com.building.service.RepairrecordService;
import com.building.sql.LiveRecordSql;
import com.building.sql.RepairrecordSql;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (Repairrecord)表服务实现类
 *
 * @author TianCai
 * @since 2023-01-14 15:07:45
 */
@Service("repairrecordService")
public class RepairrecordServiceImpl implements RepairrecordService {
    @Resource
    private RepairrecordDao repairrecordDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private DormitoryManagerDao dormitoryManagerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param rId 主键
     * @return 实例对象
     */
    @Override
    public Repairrecord queryById(Integer rId) {
        return this.repairrecordDao.queryById(rId);
    }

    /**
     * 分页查询
     *
     * @param repairrecord 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Repairrecord> queryByPage(Repairrecord repairrecord, PageRequest pageRequest) {
        long total = this.repairrecordDao.count(repairrecord);
        return new PageImpl<>(this.repairrecordDao.queryAllByLimit(repairrecord, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param repairrecord 实例对象
     * @return 实例对象
     */
    @Override
    public Repairrecord insert(Repairrecord repairrecord) {
        this.repairrecordDao.insert(repairrecord);
        return repairrecord;
    }

    /**
     * 修改数据
     *
     * @param repairrecord 实例对象
     * @return 实例对象
     */
    @Override
    public Repairrecord update(Repairrecord repairrecord) {
        this.repairrecordDao.update(repairrecord);
        return this.queryById(repairrecord.getRId());
    }

    /**
     * 通过主键删除数据
     *
     * @param rId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer rId) {
        return this.repairrecordDao.deleteById(rId) > 0;
    }

    @Override
    public ServerResponse<PageInfo<RepairrecordSql>> getPageInfoByName(Integer pageNum, Integer pageSize, String name) {
        List<Repairrecord> repairrecordSqls = this.repairrecordDao.queryAll();
        List<RepairrecordSql> repairrecordSqlList = new ArrayList<>();
        for (Repairrecord repairrecord : repairrecordSqls){
            if (studentDao.queryById(repairrecord.getSId()).getName().indexOf(name) > -1){
                RepairrecordSql repairrecordSql = new RepairrecordSql();
                repairrecordSql.setRId(repairrecord.getRId());
                repairrecordSql.setDate(repairrecord.getDate());
                repairrecordSql.setHandleTime(repairrecord.getHandleTime());
                repairrecordSql.setTitle(repairrecord.getTitle());
                repairrecordSql.setUpdateTime(repairrecord.getUpdateTime());
                repairrecordSql.setStatus(repairrecord.getStatus());
                repairrecordSql.setVersion(repairrecord.getVersion());
                repairrecordSql.setCreateTime(repairrecord.getCreateTime());
                repairrecordSql.setStudent(studentDao.queryById(repairrecord.getSId()));
                repairrecordSql.setDormitoryManager(dormitoryManagerDao.queryById(repairrecord.getDMId()));
                repairrecordSqlList.add(repairrecordSql);
            }else{
                continue;
            }
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<RepairrecordSql> repairrecordSqlPageInfo = new PageInfo<>(repairrecordSqlList);
        int size = repairrecordSqlPageInfo.getList().size();
        if (size==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        return ServerResponse.createBySuccess("查询成功,共"+size+"条数据",repairrecordSqlPageInfo);
    }

    @Override
    public ServerResponse<PageInfo<RepairrecordSql>> getPageInfo(Integer pageNum, Integer pageSize) {
        List<Repairrecord> repairrecords = this.repairrecordDao.queryAll();
        List<RepairrecordSql> repairrecordSqlList = new ArrayList<>();
        for (Repairrecord repairrecord : repairrecords){
                RepairrecordSql repairrecordSql = new RepairrecordSql();
                repairrecordSql.setRId(repairrecord.getRId());
                repairrecordSql.setDate(repairrecord.getDate());
                repairrecordSql.setHandleTime(repairrecord.getHandleTime());
                repairrecordSql.setTitle(repairrecord.getTitle());
                repairrecordSql.setUpdateTime(repairrecord.getUpdateTime());
                repairrecordSql.setStatus(repairrecord.getStatus());
                repairrecordSql.setVersion(repairrecord.getVersion());
                repairrecordSql.setCreateTime(repairrecord.getCreateTime());
                repairrecordSql.setStudent(studentDao.queryById(repairrecord.getSId()));
                repairrecordSql.setDormitoryManager(dormitoryManagerDao.queryById(repairrecord.getDMId()));
                repairrecordSqlList.add(repairrecordSql);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<RepairrecordSql> repairrecordSqlPageInfo = new PageInfo<>(repairrecordSqlList);
        int size = repairrecordSqlPageInfo.getList().size();
        if (size==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        return ServerResponse.createBySuccess("查询成功,共"+size+"条数据",repairrecordSqlPageInfo);
    }

    @Override
    public ServerResponse<Repairrecord> addRepair(Integer sid, String title) {
        Student student = this.studentDao.queryById(sid);
        if (student==null) return ServerResponse.createByErrorMessage("学生不存在!");
        Repairrecord repairrecord = repairrecordDao.queryByTitle(title);
        if (repairrecord!=null && repairrecord.getSId()==sid){
            return ServerResponse.createByErrorMessage("你这个学生怎么个事捣乱是把,这个内容已经提交过了!");
        }else{
            Repairrecord repairrecord1 = new Repairrecord();
            repairrecord1.setTitle(title);
            repairrecord1.setSId(sid);
            repairrecord1.setVersion(1);
            repairrecord1.setDate(new Date());
            repairrecord1.setCreateTime(new Date());
            repairrecord1.setUpdateTime(new Date());
            repairrecord1.setStatus(true);
            int insert = repairrecordDao.insert(repairrecord1);
            if (insert!=1) return ServerResponse.createByErrorMessage("提交失败请检查服务器!");
            return ServerResponse.createBySuccess("报修提交成功",repairrecord1);
        }
    }

    @Override
    public ServerResponse<Repairrecord> updateRepair(Integer did,Integer id) {
        DormitoryManager dormitoryManager = this.dormitoryManagerDao.queryById(did);
        if (dormitoryManager==null) return ServerResponse.createByErrorMessage("不存在此老师用户无法审批信息!");
        Repairrecord repairrecord = this.repairrecordDao.queryById(id);
        repairrecord.setUpdateTime(new Date());
        repairrecord.setVersion(repairrecord.getVersion()+1);
        repairrecord.setDMId(did);
        repairrecord.setHandleTime(new Date());
        int update = repairrecordDao.update(repairrecord);
        if (update!=1) return ServerResponse.createByErrorMessage("修改失败,请检查服务器!");
        return ServerResponse.createBySuccess("审批成功!",repairrecord);
    }

    @Override
    public ServerResponse<Repairrecord> deleteRepair(Integer id) {
        Repairrecord repairrecord = this.repairrecordDao.queryById(id);
        if (repairrecord==null) return ServerResponse.createByErrorMessage("不存在此条信息!");
        repairrecord.setStatus(false);
        repairrecord.setUpdateTime(new Date());
        repairrecord.setVersion(repairrecord.getVersion()+1);
        int update = repairrecordDao.update(repairrecord);
        if (update!=1) return ServerResponse.createByErrorMessage("删除失败,请检查服务器!");
        return ServerResponse.createBySuccess("删除信息成功!",repairrecord);
    }
}
